﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.Reflection;
using Common.SQL;
using System.Data;

namespace MRS.DBL.DAL
{
    public partial class UnitPageDAL : SQLQueryPageT<Model.UnitModel>
    {
        static log4net.ILog logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// 转换数据库结果集为Model列表
        /// </summary>
        /// <param name="dataReader"></param>
        /// <returns></returns>
        protected override Model.UnitModel[] FillData(IDataReader dataReader)
        {
            List<Model.UnitModel> arrData = new List<Model.UnitModel>();

            while (dataReader.Read())
            {
                MRS.DBL.Model.UnitModel model = new MRS.DBL.Model.UnitModel();

                if (dataReader["UnitId"] != null && dataReader["UnitId"].ToString() != "")
                {
                    model.UnitId = int.Parse(dataReader["UnitId"].ToString());
                }
                if (dataReader["UnitName"] != null && dataReader["UnitName"].ToString() != "")
                {
                    model.UnitName = dataReader["UnitName"].ToString();
                }
                if (dataReader["UnitCode"] != null && dataReader["UnitCode"].ToString() != "")
                {
                    model.UnitCode = dataReader["UnitCode"].ToString();
                }
                if (dataReader["Enable"] != null && dataReader["Enable"].ToString() != "")
                {
                    model.Enable = dataReader["Enable"].ToString();
                }
                if (dataReader["CreateDate"] != null && dataReader["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(dataReader["CreateDate"].ToString());
                }
                if (dataReader["ExpireDate"] != null && dataReader["ExpireDate"].ToString() != "")
                {
                    model.ExpireDate = DateTime.Parse(dataReader["ExpireDate"].ToString());
                }
                if (dataReader["Industry"] != null && dataReader["Industry"].ToString() != "")
                {
                    model.Industry = int.Parse(dataReader["Industry"].ToString());
                }

                arrData.Add(model);
            }

            dataReader.Close();

            return arrData.ToArray();
        }

        protected void GetUnitList(MRS.DBL.Model.UnitModel model, MRS.DBL.Model.UnitModel model2, bool asc)
        {
            string WhereCondition = this.GetQuerySqlWhereConditionByUserModel(model, model2);
            this.CountSQL = "select count(1) from unit " + WhereCondition;
            this.SQL = "select unit.UnitId, unit.UnitName, unit.UnitCode, unit.Enable, unit.CreateDate, unit.ExpireDate, unit.Industry  " + "  from unit " + WhereCondition;
            if (asc)
                this.SQL += "order by LastLoginTime asc";
            else
                this.SQL += "order by LastLoginTime desc";

        }

        // <summary>
        /// 功能：根据两个对象拼接出查询的SQL语句
        /// 说明：如果对象1中的日期时间有值，条件为大于该时间；如果对象2中的日期时间有值，条件为小于该时间
        /// </summary>
        /// <param name="userBasic1"></param>
        /// <param name="userBasic2"></param>
        /// <returns></returns>
        private string GetQuerySqlWhereConditionByUserModel(MRS.DBL.Model.UnitModel model, MRS.DBL.Model.UnitModel model2)
        {
            StringBuilder sqlBuilder = new StringBuilder();

            if (model.UnitId > 0)
                sqlBuilder.AppendFormat((sqlBuilder.Length > 0 ? " and " : "") + " unit.UnitId={0} ", model.UnitId);
            if (!string.IsNullOrEmpty(model.UnitName))
                sqlBuilder.AppendFormat((sqlBuilder.Length > 0 ? " and " : "") + " unit.UnitName like '%{0}%' ", Common.SQL.SQLHelper.GetLikeCondition(model.UnitName));
            if (!string.IsNullOrEmpty(model.UnitCode))
                sqlBuilder.AppendFormat((sqlBuilder.Length > 0 ? " and " : "") + " unit.UnitCode like '%{0}%' ", Common.SQL.SQLHelper.GetLikeCondition(model.UnitCode));
            if (!string.IsNullOrEmpty(model.Enable))
                sqlBuilder.AppendFormat((sqlBuilder.Length > 0 ? " and " : "") + " unit.Enable like '%{0}%' ", Common.SQL.SQLHelper.GetLikeCondition(model.Enable));
            if (model.CreateDate > DateTime.MinValue)
                sqlBuilder.AppendFormat((sqlBuilder.Length > 0 ? " and " : "") + " unit.CreateDate >= '{0}' ", model.CreateDate);
            if (model.ExpireDate > DateTime.MinValue)
                sqlBuilder.AppendFormat((sqlBuilder.Length > 0 ? " and " : "") + " unit.ExpireDate >= '{0}' ", model.ExpireDate);
            if (model.Industry > 0)
                sqlBuilder.AppendFormat((sqlBuilder.Length > 0 ? " and " : "") + " unit.Industry={0} ", model.Industry);

            //第二个model
            if (model2 != null && model2.CreateDate > DateTime.MinValue)
                sqlBuilder.AppendFormat((sqlBuilder.Length > 0 ? " and " : "") + " unit.CreateDate <= '{0}' ", model2.CreateDate);
            if (model2 != null && model2.ExpireDate > DateTime.MinValue)
                sqlBuilder.AppendFormat((sqlBuilder.Length > 0 ? " and " : "") + " unit.ExpireDate <= '{0}' ", model2.ExpireDate);


            if (sqlBuilder.Length > 0)
                return " where " + sqlBuilder.ToString();
            else return null;

        }
    }
}
